package com.itheima.edu.info.managers.dao;

import com.itheima.edu.info.managers.domain.Teacher;

public class TeacherDao {
    private static Teacher[] teachers = new Teacher[10];

    public boolean addTeacher (Teacher teacher ) {
        //创建一个学生数组，动态初始化
        //Student[] students=new Student[10];
        //设置他的索引不存在，遍历数组
        //2遍历数组取出每一个元素，判断是否是null
        int index = -1;
        for (int i = 0; i <teachers.length; i++) {
            Teacher tea = teachers[i];
            //如果为null，让index变量记录当前索引位置，并使用break结束循环遍历
            if (tea == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            //装满了
            return false;
        } else {
            //可以添加
            teachers[index] = teacher;
            return true;
        }

    }

    public static Teacher[] findAllTeacher() {
        return teachers;
    }

    public void deleteTeacher(String delId) {
        int index = getIndex(delId);

        teachers[index] = null;
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher tea = teachers[i];
            if (tea != null && tea.getId().equals(id)) {
                index = i;
            }
        }
        return index;

    }

    public void updateTeacher(Teacher tea, String updateId) {
        int index = getIndex(updateId);
        teachers[index] = tea;
    }
}
